﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using MySql.Data.MySqlClient;
namespace SQLTask2
{
    public partial class GraphsForm : Form
    {
        private MySqlConnection pConn;
        private MySqlDataAdapter da = null;
        private DataSet ds2 = null;
        private string[] queries = {//Fill main grid
                                    @"SELECT Buy,
                                             Sell,
                                             Date
                                     FROM rates WHERE (Date BETWEEN '{0}' AND '{1}')
                                     AND (F_Currency_ID='{2}') AND (S_Currency_ID='{3}') ORDER BY Date",
                                    //Fill countries combobox
                                    @"SELECT Buy,
                                             Date
                                     FROM rates WHERE (Date BETWEEN '2013-05-09' AND '2013-05-30')
                                        AND (F_Currency_ID='1') AND (S_Currency_ID='3') ORDER BY Date",
                                    @"SELECT Name,
                                             Currency_ID
                                     FROM currencies",
                                   };

        public GraphsForm(MySqlConnection conn)
        {
            InitializeComponent();
            pConn = conn;
        }

        private void GraphsForm_Load(object sender, EventArgs e)
        {
            DataTable ds = new DataTable();
            
                try
                {
                    (new MySqlDataAdapter(queries[2], pConn)).Fill(ds);
                    //(new MySqlDataAdapter(queries[2], pConn)).Fill(ds.Tables[1]);
                    cbFrom.DataSource = ds;
                    cbTo.DataSource = ds.Copy();
                    cbFrom.DisplayMember =  cbTo.DisplayMember="Name";
                    cbFrom.ValueMember = cbTo.ValueMember = "Currency_ID";
                }
                catch (MySqlException sqlExc)
                {
                    MessageBox.Show(sqlExc.Message);
                }
                ds2 = new DataSet();
                chart1.DataSource = ds2;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            chart1.Series[0].Points.Clear();
            chart1.Series[1].Points.Clear();
            ds2.Clear();
            try
            {
                    (new MySqlDataAdapter(String.Format(queries[0],
                                                        dateFrom.Value.ToString("yyyy-MM-dd"),
                                                        dateTo.Value.ToString("yyyy-MM-dd"),
                                                        cbFrom.SelectedValue,
                                                        cbTo.SelectedValue), pConn)).Fill(ds2,"Query");
            }
            catch (MySqlException sqlExc)
            {
                MessageBox.Show(sqlExc.Message);
            }

               // chart1.Series[0].ChartType = SeriesChartType.Line;
               // chart1.Series[0].XValueType = ChartValueType.DateTime;
               // chart1.Series[0].XValueMember = "Date";

                if (checkBox1.Checked)
                    chart1.Series[0].YValueMembers = "Buy";
                else
                    chart1.Series[0].YValueMembers = "";
                if (checkBox2.Checked)
                    chart1.Series[1].YValueMembers = "Sell";
                else
                    chart1.Series[1].YValueMembers = "";
                chart1.DataBind();

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            button1_Click(this, e);
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            button1_Click(this, e);
        }

    }
}
